﻿@namespace ThingsGateway.Gateway.Razor
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Gateway.Application
@attribute [Authorize]
@attribute [RolePermission]
@inherits ComponentDefault


<div style="height: var(--table-height);padding: 2px 5px 10px 5px;">

    <AdminTable @ref=table TItem="Device" AutoGenerateColumns="true" ShowAdvancedSearch=false
                AllowResizing="true" IsFixedHeader=true IsMultipleSelect=true SearchModel=SearchModel
                ShowExtendButtons=true ShowExportButton ScrollingDialogContent
                ShowDefaultButtons=true ExtendButtonColumnWidth=150
                OnQueryAsync="OnQueryAsync" IsPagination=true
                OnSaveAsync="Save" OnDeleteAsync="Delete">

        <TableColumns>
            <TableColumn @bind-Field="@context.PluginName" Align="Alignment.Center" Filterable=true Sortable=true>
                <Template Context="pluginName">
                    @PluginServiceUtil.GetFileNameAndTypeName(pluginName?.Value).TypeName
                </Template>
            </TableColumn>

            <TableColumn @bind-Field="@context.ChannelId" Align="Alignment.Center" Sortable=true>
                <Template Context="channelId">
                    @{
                        ChannelDict.TryGetValue(channelId.Value, out var channel);
                    }
                    @channel?.Name
                </Template>
            </TableColumn>

            <TableColumn @bind-Field="@context.RedundantDeviceId" Align="Alignment.Center" Sortable=true>
                <Template Context="deviceId">
                    @{
                        string device = null;
                        if (deviceId.Value.HasValue)
                            DeviceDict.TryGetValue(deviceId.Value.Value, out device);
                    }
                    @device
                </Template>

            </TableColumn>

        </TableColumns>
        <EditTemplate Context="context">
            <DeviceEditComponent Model=context PluginType=PluginType PluginNames="PluginNames" DeviceDict=DeviceDict RedundantDevicesQuery="OnRedundantDevicesQuery"></DeviceEditComponent>
        </EditTemplate>


        <ExportButtonDropdownTemplate Context="ExportContext">
            <Button class="dropdown-item" OnClick="() => ExcelExportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导出"))>
                <i class="fas fa-file-export"></i>
                <span>@DefaultLocalizer["TablesExportButtonExcelText"]</span>
            </Button>
            <Button class="dropdown-item" OnClick="() => ExcelImportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导入"))>
                <i class="fas fa-file-import"></i>
                <span>@DefaultLocalizer["TablesImportButtonExcelText"]</span>
            </Button>
        </ExportButtonDropdownTemplate>
        <TableToolbarTemplate>
            <TableToolbarButton TItem="Device" IsShow=@(AuthorizeButton(AdminOperConst.Edit))
                                Color=Color.Info Text="@DefaultLocalizer["BatchEdit"]"
                                OnClickCallback=@(BatchEdit) />

            <TableToolbarPopConfirmButton TItem="Device"
                                          Color=Color.Warning Text="@Localizer["Clear"]" IsShow=@(AuthorizeButton("清空"))
                                          IsAsync OnConfirm=@(ClearDeviceAsync) />
        </TableToolbarTemplate>
    </AdminTable>
</div>

@code {
    AdminTable<Device> table;
}
